using System.Globalization;
using System.Web;
using System.Web.Compilation;
using System.Resources;
using System.Collections;
using System.Collections.Specialized;

namespace VideoSeller.Core.Localization
{
	/// <summary>
    /// Custom Resource Provider
    /// </summary>
    public class CustomResourceProvider : IResourceProvider
    {
        private string virtualPath;
        private string className;
        private IDictionary resourceCache;

        /// <summary>
        /// Initializes a new instance of the <see cref="CustomResourceProvider"/> class.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        /// <param name="className">Name of the class.</param>
        public CustomResourceProvider(string virtualPath, string className)
        {
            this.virtualPath = virtualPath;
            this.className = className;
        }

        /// <summary>
        /// Gets the resource cache.
        /// </summary>
        /// <param name="cultureName">Name of the culture.</param>
        /// <returns></returns>
        private IDictionary GetResourceCache(string cultureName)
        {
	        if (HttpContext.Current.Application["ShouldUpdateResourceProviderCache"] != null &&
	            (bool) HttpContext.Current.Application["ShouldUpdateResourceProviderCache"])
	        {
		        resourceCache = null;
		        HttpContext.Current.Application["ShouldUpdateResourceProviderCache"] = null;
	        }

	        //if (resourceCache == null)
            {
                resourceCache = new ListDictionary();
            }

			var resourceCacheKey = string.Concat(cultureName, virtualPath, className);
			var resourceDict = resourceCache[resourceCacheKey] as IDictionary;

            if (resourceDict == null || resourceDict.Count == 0)
            {
                resourceDict = SqlResourceHelper.GetResources(virtualPath, className, cultureName, false, null);
				resourceCache[resourceCacheKey] = resourceDict;
            }

            return resourceDict;
        }

        /// <summary>
        /// Returns a resource object for the key and culture.
        /// </summary>
        /// <param name="resourceKey">The key identifying a particular resource.</param>
        /// <param name="culture">The culture identifying a localized value for the resource.</param>
        /// <returns>
        /// An <see cref="T:System.Object"></see> that contains the resource value for the resourceKey and culture.
        /// </returns>
        object IResourceProvider.GetObject(string resourceKey, CultureInfo culture)
        {
	        var cultureName = culture != null ? culture.Name : CultureInfo.CurrentUICulture.Name;
	        var cache = GetResourceCache(cultureName);
			var value = cache[resourceKey];

			if (value == null)
            {
                SqlResourceHelper.AddResource(resourceKey, virtualPath, className, cultureName, string.Empty);
				value = GetResourceCache(cultureName)[resourceKey];
            }

            return value;
        }

        /// <summary>
        /// Gets an object to read resource values from a source.
        /// </summary>
        /// <value></value>
        /// <returns>The <see cref="T:System.Resources.IResourceReader"></see> associated with the current resource provider.</returns>
        IResourceReader IResourceProvider.ResourceReader
        {
	        get { return new CustomResourceReader(GetResourceCache(null)); }
        }
    }
}